package com.springboot.demo.mapper;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.springboot.demo.entity.Inbox;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.ExcludeSuperclassListeners;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author wubc683
 * @desc
 * @date 2021-09-01 10:12
 */
public interface InboxMapper {
    @Update("update web_inbox set readed = #{readed} where id = #{id}")
    void updateMsgReadedByMsgId(Long id,Integer readed);

    @SelectProvider(type = SqlMapper.class,method = "selectInbox")
    List<Inbox> getMsg(Long uid, Integer readed, Integer type, Date startDate, Date endDate, String search,
                       String sort);

    @Delete("delete from web_inbox where id = #{id}")
    void deleteMsgById(Long id);

    @Insert("insert into web_inbox(ownerId,type,content,readed,createDate,needReply,replyState,replyUrl," +
            "encryptData)" + " values" + "(#{ownerId},#{type},#{content}," +
            "#{readed},#{createDate},#{needReply},#{replyState},#{replyUrl},#{encryptData})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void saveInbox(Inbox inbox);

    @Select("select id,ownerId,type,content,readed, createDate,needReply,replyState,replyUrl,encryptData from " +
            "web_inbox where id = #{msgId}")
    Inbox getMsgInfo(Long msgId);

    @Update("update web_inbox set replyState = #{state} where id = #{msgId}")
    void updateInboxReply(Long msgId, Integer state);

//    @Insert("<script> insert into web_user_box(uid,msgId) values" + "<foreach item = 'item' collection='userIds' " +
//            "separator=','>" + "(#{item},#{id})" + "</foreach> </script>")
//    @Insert({"<script>"+ "insert into web_connection(connectId,sceneId,name,driveType,port)"+
//            "values"+ "<foreach item='item' collection='connList' separator=','>"+
//            "(#{item.connectId},#{sid,jdbcType=BIGINT},#{item.name},#{item.driveType},#{item.port})"+ "</foreach>"+
//            "</script>"})
//    void saveUsersInbox(List<Long> userIds, Long id);

    @Update("update web_inbox set readed = #{readed},replyState=#{replyState} where id = #{id}")
    void updateInboxReadedAndState(Inbox msgInfo);

    @Delete("delete from web_inbox where ownerId = #{uid}")
    void deleteMsgByUserId(Long uid);

    @Update("update web_inbox set readed=#{readed} where ownerId = #{uid}")
    void updateAllMsgReadedByUserId(Long uid,Integer readed);

    @Select("select id from web_inbox where ownerId = #{uid}")
    List<Long> getAllMsgByUserId(Long uid);

    @Insert("<script> insert into web_inbox(ownerId,type,content,readed,createDate,needReply,replyState," +
            "replyUrl," +
            "encryptData) values <foreach item='item' collection='inboxList' separator=','> (#{item.ownerId},"+
            "#{item.type},#{item.content}," +
            "#{item.readed},#{item.createDate},#{item.needReply},#{item.replyState},#{item.replyUrl}," +
            "#{item.encryptData}) </foreach> </script>")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void saveInboxes(List<Inbox> inboxList);

    @Select("select id,ownerId,type,content,#{code} as code ,readed, createDate,needReply,replyState,replyUrl," +
            "encryptData from " + "web_inbox where ownerId = #{userId} and id = 252")
    List<Inbox> getAllUnconfirmedMsgInfoByUserId(Long userId,Integer code);
}
